﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cloak.NUnit;
using Cloak.Reflection;
using Moq;
using NUnit.Framework;
using Ruleweaver.Rules;

namespace Ruleweaver.Conditions.Schemas
{
	public class GetConditionFromRepositoryWithTwo : Behavior
	{
		private Rule _rule0;
		private Rule _rule1;
		private Mock<IConditionSource> _sourceMock;
		private ConditionRepository _repository;
		private Condition _repositoryCondition;

		protected override void Given()
		{
			_rule0 = Rule.Check(Reflect.Func<ICheckable<int>, Check<int>>(Checkable.IsPositive));
			_rule1 = Rule.Check(Reflect.Func<ICheckable<int>, Check<int>>(Checkable.IsEven));

			var condition0 = new Condition(_rule0, typeof(int));
			var condition1 = new Condition(_rule1, typeof(int));

			_sourceMock = new Mock<IConditionSource>();

			_sourceMock.Setup(source => source.GetConditions()).Returns(new[] { condition0, condition1 });

			_repository = new ConditionRepository(_sourceMock.Object);
		}

		protected override void When()
		{
			_repositoryCondition = _repository.GetCondition(new ConditionKey(typeof(int)));
		}

		[Then]
		public void RuleHasAndType()
		{
			Assert.AreEqual(RuleType.And, _repositoryCondition.Rule.Type);
		}

		[Then]
		public void FirstRuleIsFirstAndOperand()
		{
			Assert.AreEqual(_rule0, (((CompositeRule) _repositoryCondition.Rule).Rules.First()));
		}

		[Then]
		public void SecondRuleIsSecondAndOperand()
		{
			Assert.AreEqual(_rule1, (((CompositeRule) _repositoryCondition.Rule).Rules.Skip(1).Single()));
		}
	}
}